package com.lylg.picture_share.controller;

import com.github.pagehelper.PageInfo;
import com.lylg.picture_share.entity.Image;
import com.lylg.picture_share.entity.ImageType;
import com.lylg.picture_share.entity.Manager;
import com.lylg.picture_share.service.impl.ImageServiceImpl;
import com.lylg.picture_share.service.impl.ImageTypeServiceImpl;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/image")
public class ImageController {
    @Autowired
    private ImageServiceImpl imageService;
    private PageInfo<Image> pageInfo;
    @Autowired
    private ImageTypeServiceImpl imageTypeService;

    @RequestMapping("/toimage")
    public String toImage(){
        return "/image/image";
    }
    @RequestMapping("/listui")
    public String list(Map<String,Object> model, Image queryBean, Integer currentPage){
        //因为页面首次加载的时候传递不过来页码信息
        if(currentPage==null){

            currentPage=1;
        }
        else{

            if(currentPage>pageInfo.getPages()){
                currentPage=pageInfo.getPages();
            }
            //防止点击上一页越界
            if(currentPage==0){
                currentPage=1;
            }
        }

        Integer pageSize=5;
//        pageInfo=videoService.getAllVideos(queryBean,currentPage,pageSize);
          pageInfo = imageService.getAllImages(queryBean, currentPage, pageSize);
        model.put("pb",pageInfo);
        model.put("qb",queryBean);
        return "/image/image";
    }
    @RequestMapping("/passui")
    public String doPassui(Map<String,Object> model,Integer image_id){

//        Video video = videoService.getVideoById(video_id);
        Image image = imageService.getImageById(image_id);
        model.put("image",image);
        //请求转发

        return "/image/pass.html";

    }
    @RequestMapping("/pass")
    public String doPass(Image image, HttpSession session){
//        video.setVideo_status("已审核");
        image.setImage_status("已审核");

        //审核人的id(当前登录的人的id)
        //
        Integer managerId=null;
        if(session.getAttribute("loginmanager")!=null){

            managerId=((Manager)session.getAttribute("loginmanager")).getManager_id();
        }
//        video.setManager_id(managerId);
//
//        videoService.passVideo(video);
        image.setManager_id(managerId);
        imageService.passImage(image);

        return "redirect:/image/listui";

    }
    @RequestMapping("/addui")
    public String doVideoUI(Map<String,Object> model){
        List<ImageType> l = imageTypeService.selectAllImageType();
        model.put("types",l);
        return "/image/add";
    }
    @GetMapping("/toindex")
    public String toSuccess(){
        return "/image/index";
    }
    @RequestMapping("/save")
    public String doSave(@RequestParam("image_file") MultipartFile multipartFile , Image image){




        //得到上传的文件的文件名称（1.mp4）
        String fileName = multipartFile.getOriginalFilename();
        //获取文件后缀名(.mp4)
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //重新生成文件名  拼接新文件前缀名和后缀名
        String newFileName = UUID.randomUUID()+suffixName;
        //以上三行代码得到了更改之后的新文件名称

        //获得静态资源的一个路径()
        String filePath = ClassUtils.getDefaultClassLoader().getResource("static").getPath()+"/upimg";
        File dir=new File(filePath);
        //路径指定的目录不存在，就创建目录
        if(!dir.exists()){
            dir.mkdir();
        }
        //在指定目录下根据新文件的名称创建了一个file类型的对象
        File dest = new File(dir , newFileName);
        System.out.println(dest.getAbsolutePath());

        try {
            //将上传上来的文件按照新文件名称存放到指定的路径下
            multipartFile.transferTo(dest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //将上传文件的url要保存到数据库中
          image.setImage_url("upimg/"+newFileName);
          image.setImage_good(0);
          image.setImage_play(0);
          image.setImage_status("未审核");
          image.setImage_time(new Date());
          imageService.add(image);

        return "redirect:/image/listui";


    }
      @ResponseBody
      @RequestMapping("/desc")
    public List<Image> doImagesByDesc(Model model){
          List<Image> images = imageService.selectImagesByDesc();
          System.out.println(images);
          model.addAttribute("images",images);
          return images;
      }
    @ResponseBody
    @RequestMapping("/byname")
    public List<Image> select1(Model model,Map<String,Object> map){
        Image image1 = new Image();
        image1.setImage_name("女");

        List<Image> images = imageService.selectImagesByName(image1);
        System.out.println(images);
        model.addAttribute("images",images);
        return images;
    }

}
